<?php


namespace app\blog\controller\v1;


use app\BaseController;
use app\blog\model\MessageModel;
use app\blog\service\MessageService;
use app\blog\Validate\message\GetMessageListValidate;
use app\blog\Validate\message\SendMessageValidate;

/**
 * 留言板
 * Class Message
 * @package app\blog\controller\v1
 */
class Message extends BaseController
{
    /**
     * 留言接口
     * @return \think\response\Json
     */
    public function sentMessage() {
        $param = (new SendMessageValidate())->validateParam();
        $param['add_time'] = getDateTime();
        $bool = (new MessageModel())->save($param);
        if($bool) {
            return successJson(true, "添加成功");
        }
        return errorJson(60001, "添加失败");
    }

    /**
     * 获取留言列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMessageList() {
        $param = (new GetMessageListValidate())->validateParam();
        $messageModel = new MessageModel();
        $lists = $messageModel->where('article_id', '=', $param['article_id'])
            ->where('status', 1)
            ->hidden(['status', 'read_status'])
            ->select()
            ->toArray();
        $messageService = new MessageService();
        $messageList = $messageService->MessageList($lists);
        return successJson($messageList);
    }

    /**
     * 获取留言列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getBlogMessageList() {
//        $param = (new GetMessageListValidate())->validateParam();
        $messageModel = new MessageModel();
        $lists = $messageModel->where('article_id', '=', -1)
            ->where('status', 1)
            ->hidden(['status', 'read_status'])
            ->select()
            ->toArray();
        $messageService = new MessageService();
        $messageList = $messageService->MessageList($lists);
        return successJson($messageList);
    }

}